#include "TabPanel2.h"
#include "base/Utility.h"
#define SetAttr(AttrName) SetAttribute(AttrName, ele->Attribute(AttrName));

bool TabPanel2::OnInit()
{
	return true;
}

void TabPanel2::OnDraw()
{
	childs[curActiveIndex].second->pos.x.val = pos.x.val;
	childs[curActiveIndex].second->pos.y.val = pos.y.val;
	childs[curActiveIndex].second->OnDraw();
}

void TabPanel2::onMouseButtonUp(int button, int x, int y)
{ 
	childs[curActiveIndex].second->onMouseButtonUp(button,x,y);
}

void TabPanel2::onMouseButtonDown(int button, int x, int y)
{
	childs[curActiveIndex].second->onMouseButtonDown(button,x,y);
}

void TabPanel2::onMouseMove(int x, int y)
{
	childs[curActiveIndex].second->onMouseMove(x, y);
}

bool TabPanel2::SetAttribute(const char* attrName, const char* attrVal)
{
	if(attrName == nullptr)
		return false;
	if(Control::SetAttribute(attrName,attrVal))
		return true;

	return false;
}

void TabPanel2::ParseXmlElement(TiXmlElement* ele)
{
	OnInit();
	Control::ParseXmlElement(ele);
	if (strcmp(ele->Value(), "TabPanel2") == 0)
	{
		auto& vattr = Utility::GetTiXmlElementAttributes(ele);
		for (int i = 0; i < vattr.size(); i++) {
			SetAttr(vattr[i].data());
		}
		ele = ele->FirstChildElement();
		while (ele)
		{
			Control* ctrl = CreateControl(ele->Value());
			ctrl->DoParseXmlElement(ele);
			ctrl->rootWindow = this->rootWindow;
			TString tstr = ctrl->name.val.data();

			TiXmlAttribute* ta = ele->FirstAttribute();
			while (ta)
			{
				ctrl->SetAttribute(ta->Name(), ta->Value());
				ta = ta->Next();
			}

			if (ctrl)
			{
				childs.push_back(std::make_pair(tstr, ctrl));
			}
			ele = ele->NextSiblingElement();
			ctrl->OnInit();
		}
	}
}

Control* TabPanel2::CreateControl(std::string sControlClassName)
{
	Control* ctrl = dynamic_cast<Control*>(BlueprintScriptLibrary::BlueprintDataPool::GetInstance()->createInstFuncs[sControlClassName]());
	ctrl->rootWindow = rootWindow;
	ctrl->parent = std::shared_ptr<Control>(this);
	return ctrl;
}

